package com.briup.house;

import java.sql.SQLException;
import java.util.List;

public class HouseView {
    //修改房屋
    private HouseServer hs=new HouseServer();
    private boolean flag=true;
    public void addHouse() throws SQLException, ClassNotFoundException {
        System.out.println("**********新增房屋信息*********");
        System.out.println("id:");
        int id=Util.readInt();
        System.out.println("姓名:");
        String name=Util.readString(10);
        System.out.println("电话号码:");
        String phone=Util.readString(11);
        System.out.println("地址");
        String address=Util.readString(20);
        System.out.println("月租:");
        int rent=Util.readInt();
        System.out.println("出租状态:");
        String state=Util.readString(5);
        //把数据传输在对象中
        House house =
                new House(id,name,phone,address,rent,state);
        System.out.println(house);
        if(hs.add(house)){
            System.out.println("新增成功");
        }else {
            System.out.println("新增失败");
        }

    }
//查找房屋
    public void selectbyId() throws SQLException, ClassNotFoundException {
        System.out.println("**********查找房屋信息*********");
        System.out.println("请输入你要查询的房屋的id(-1退出):");
        int id = Util.readInt();
        if (id==-1){
            System.out.println("你放弃了本次查找");
            return;
        }
        if (hs.selectByID(id)){
            System.out.println("查找成功");
        }else {
            System.out.println("查找失败");
        }
    }
    //删除房屋
    public void deleteHouse() throws SQLException, ClassNotFoundException {
        System.out.println("**********删除房屋信息*********");
        System.out.println("请选择你要删除的房屋编号(-1退出)");
        int id=Util.readInt();
        //if ---Y  hs.delete(id);  else break
        if (id==-1){
            System.out.println("你放弃了删除");
            return;
        }
        char del=Util.readConfirmSelection();
        if (del=='Y'){
            if (hs.delete(id)){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        }else {
            System.out.println("放弃删除");
        }
    }

    public void reciveHouse() throws SQLException, ClassNotFoundException {
        System.out.println("**********修改房屋信息*********");
        System.out.println("id:");
        int id=Util.readInt();
        System.out.println("姓名:");
        String name=Util.readString(10);
        System.out.println("电话号码:");
        String phone=Util.readString(11);
        System.out.println("地址");
        String address=Util.readString(20);
        System.out.println("月租:");
        int rent=Util.readInt();
        System.out.println("出租状态:");
        String state=Util.readString(5);
        //把数据传输在对象中
        House house =
                new House(id,name,phone,address,rent,state);
        if(hs.update(house)){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
    }
//展示房屋列表
    public void seleclAllHouse() throws SQLException, ClassNotFoundException {
        System.out.println("**********查看房屋列表*********");
        System.out.println("编号   "+"\t房主    "+"\t电话  "+
                "\t地址   "+"\t租金   "+"\t状态 ");
        //获取房屋列表
        List<House> list = hs.list();
        for (House house : list) {
            System.out.println(house);
        }
        System.out.println("**********房屋列表展示完成**********");
    }
    //退出
    public void exit(){
        char execCheck = Util.readConfirmSelection();
        if (execCheck=='Y'){
            flag=false;
            System.out.println("-----------"+flag);

        }
    }


        public void menuMain() throws SQLException, ClassNotFoundException {
            do {
                System.out.println("\n*********房屋出租系统*********");
                System.out.println("\t\t\t1新增房屋");
                System.out.println("\t\t\t2查找房屋");
                System.out.println("\t\t\t3删除房屋");
                System.out.println("\t\t\t4修改房屋");
                System.out.println("\t\t\t5房屋列表");
                System.out.println("\t\t\t0退出");
                System.out.println("请输入(1-6)");
                char key = Util.readChar();
                switch (key){
                    case '1':
                        addHouse();
                        break;
                    case '2':
                        selectbyId();
                        break;
                    case '3':
                        deleteHouse();
                        break;
                    case '4':
                        reciveHouse();
                        break;
                    case '5':
                        seleclAllHouse();
                        break;
                    case '6':
                        exit();
                        break;
                }

            }while (flag);
        }
}
